
//
//  OrderListViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "IdAndMaOrderListViewController.h"
#import "OrderGoodsCell.h"
#import "UserInfoModel.h"
#import "OrderListModel.h"
#import "LGOrderSectionCell.h"
#import "LGOrderGoodsCell.h"
#import "LGOrderBottomCell.h"
#import "LGOrderMultipleCell.h"
#import "LGCommentViewController.h"
#import "OrderLogisticsViewController.h"
#import "IdentifyAndMaintainDetailVC.h"
#import "AddSendInfoViewController.h"


NSString * const IdAndMaOrderSectionCellIdentifier = @"IdAndMaOrderSectionCellIdentifier";
NSString * const IdAndMaOrderGoodsCellIdentifier = @"IdAndMaOrderGoodsCellIdentifier";
NSString * const IdAndMaOrderMultipleCellIdentifier = @"IdAndMaOrderMultipleCellIdentifier";
NSString * const IdAndMaOrderBottomCellIdentifier = @"IdAndMaOrderBottomCellIdentifier";
@class XLPagerTabStripViewController;
@interface IdAndMaOrderListViewController ()
@property(nonatomic,assign) TableViewControllerStyle style;
@property(nonatomic,assign) OrderVCType type;
@property(nonatomic,strong) OrderListModel * listModel;
@end

@implementation IdAndMaOrderListViewController
- (instancetype)initWithStyle:(TableViewControllerStyle)style withType:(OrderVCType )type{
    self = [super init];
    if (self) {
        self.style = style;
        self.type = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNormalTableViewNetworkData) name:@"UPDATE_IdAndMaOrder_LIST" object:nil];
 
}
- (void)updateList:(id)sender{
    [self requestOrderList];
}
- (void)requestOrderList{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    switch (self.style) {
        case TableViewControllerStyle_all:
            
            break;
        case TableViewControllerStyle_waitPayment:
            [params addEntriesFromDictionary:@{@"status":@"0"}];
            break;
        case TableViewControllerStyle_waitSend:
            [params addEntriesFromDictionary:@{@"status":@"10"}];
            break;
        case TableViewControllerStyle_waitMaintain:
            [params addEntriesFromDictionary:@{@"status":@"11"}];
            break;
        case TableViewControllerStyle_waitIdentify:
            [params addEntriesFromDictionary:@{@"status":@"11"}];
            break;
            
        case TableViewControllerStyle_waitComments:
            [params addEntriesFromDictionary:@{@"status":@"30"}];
            break;
        case TableViewControllerStyle_waitGoods:
            [params addEntriesFromDictionary:@{@"status":@"20"}];
            break;
            
        default:
            break;
    }
    NSString * order_type = @"";
    if (self.type == OrderVCType_identify) {
        order_type = @"1";
    }else if (self.type == OrderVCType_maintain){
         order_type = @"2";
    }
    [params addEntriesFromDictionary:@{@"order_type":order_type}];
    [params addEntriesFromDictionary:@{@"p":@(self.currentPageNo)}];
    [params addEntriesFromDictionary:@{@"psize":@(self.pageCount)}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"/Otherorder/getList" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject && !error) {
            if (strongSelf.currentPageNo == 0) {
                strongSelf.listModel = nil;
                strongSelf.listModel = [[OrderListModel alloc]initWithDictionary:responseObject error:nil];
                strongSelf.currentPageNo ++;
            }else{
                if (responseObject[@"data"] && [responseObject[@"data"] count] > 0 ) {
                    [strongSelf.listModel.data addObjectsFromArray: responseObject[@"data"]];
                    strongSelf.currentPageNo ++;
                }
            }
            
        }
        [strongSelf updateTableView];
     
    }];
    
}
- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44);
    return tableViewFrame;
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}

- (void)registerCell{
    
   
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderSectionCell class]) bundle:nil] forCellReuseIdentifier:IdAndMaOrderSectionCellIdentifier];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderGoodsCell class]) bundle:nil] forCellReuseIdentifier:IdAndMaOrderGoodsCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderMultipleCell class]) bundle:nil] forCellReuseIdentifier:IdAndMaOrderMultipleCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LGOrderBottomCell class]) bundle:nil] forCellReuseIdentifier:IdAndMaOrderBottomCellIdentifier];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger section = 0;
    section = [self.listModel.data count];
    return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 3;
    return rows;
}
- (NSString *)titleForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    NSString * tabTitle = @"";
    switch (self.style) {
        case TableViewControllerStyle_all:
            tabTitle = @"全部";
            break;
        case TableViewControllerStyle_waitMaintain:
            tabTitle = @"待养护";
            break;
        case TableViewControllerStyle_waitIdentify:
            tabTitle = @"待鉴定";
            break;
        case TableViewControllerStyle_waitComments:
            tabTitle = @"待评价";
            break;
        case TableViewControllerStyle_waitPayment:
            tabTitle = @"待付款";
            break;
        case TableViewControllerStyle_waitSend:
            tabTitle = @"待寄件";
            break;
        case TableViewControllerStyle_waitGoods:
            tabTitle = @"待收货";
            break;
        default:
            break;
    }
    return   tabTitle;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  height = 0;
    if (indexPath.row == 0) {
        height = 50;
    }else if (indexPath.row == 1){
        height = 70;
    }else if (indexPath.row == 2){
        height = 50;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    OrderModel * model = self.listModel.data[indexPath.section];
    if (indexPath.row == 0) {
        LGOrderSectionCell * tempCell = [tableView dequeueReusableCellWithIdentifier:IdAndMaOrderSectionCellIdentifier];
        ViewSectionType type =  ViewSectionType_normal;
        if (self.type == OrderVCType_identify) {
            type = ViewSectionType_Identification;
        }
        else if (self.type == OrderVCType_maintain) {
            type = ViewSectionType_maintenance;
        }
        [tempCell setupOrderData:model withType:type];
        cell = tempCell;
    }else if(indexPath.row == 1){
        
        if ([model.goods_item count] >1) {
            LGOrderMultipleCell * tempCell = [tableView dequeueReusableCellWithIdentifier:IdAndMaOrderGoodsCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }else{
            LGOrderGoodsCell * tempCell = [tableView dequeueReusableCellWithIdentifier:IdAndMaOrderMultipleCellIdentifier];
            [tempCell setupOrderData:model];
            cell = tempCell;
        }
        
    }else if (indexPath.row == 2){
        LGOrderBottomCell * tempCell = [tableView dequeueReusableCellWithIdentifier:IdAndMaOrderBottomCellIdentifier];
        ViewType type = ViewType_normal;
        if (self.type == OrderVCType_identify) {
            type = ViewType_Identification;
        }else if (self.type == OrderVCType_maintain){
            type = ViewType_maintenance;
        }
        [tempCell setupOrderData:model withViewType:type];
        tempCell.btnBlock = ^(OrderBottomStyle style, NSString *orderId) {
            switch (style) {
                case OrderBottomStyle_waitPayment:
                    [self gotoPaymentVC:orderId];
                    break;
                case OrderBottomStyle_waitComment:
                    [self gotoCommentVC:orderId];
                    break;
                case OrderBottomStyle_waitGoods:
                    [self confirmGoods:orderId];
                    break;
                case OrderBottomStyle_waitIdOrMtn:
                    [self gotoLogisticsVC:orderId];
                    break;
                case OrderBottomStyle_waitSend:
                    [self gotoAddSendInfo:orderId];
                    break;
                    
                default:
                    break;
            }
        };
        cell = tempCell;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * orderId = @"";
    OrderModel * model = self.listModel.data[indexPath.section];
    NSInteger  status  = [model.status integerValue];
    IdentifyAndMaintainDetailType type = IdentifyAndMaintainDetailType_normal;
    if (self.type == OrderVCType_identify) {
        type = IdentifyAndMaintainDetailType_Identify;
    }else if (self.type == OrderVCType_maintain){
         type = IdentifyAndMaintainDetailType_Maintain;
    }

    orderId =[NSString stringWithFormat:@"%@",model.order_id];
    IdentifyAndMaintainDetailVC * detailVC = [[IdentifyAndMaintainDetailVC alloc] initWithStyle:status withVCType:type withOrderId:orderId];
    [self pushViewController: detailVC];
}


#pragma mark ----
//   页面加载调用 子类重写
- (void)getNormalTableViewNetworkData{
    NSLog(@"进入页面请求 子类重写");
    self.currentPageNo = 0;
    self.pageCount = 10;
   
    [self requestOrderList];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestOrderList];
}
- (NSInteger )getNetworkTableViewDataCount{
    
    return  [self.listModel.data count];
}

#pragma mark ---
- (void)gotoCommentVC:(NSString *) orderId{
    
    LGCommentViewController * commentVC = [[LGCommentViewController alloc]initWithOrderId:orderId withVCType:LGCommentVCType_idOrMatn];
    [self pushViewController:commentVC];
}

- (void)gotoLogisticsVC:(NSString *) orderId{
    OrderLogisticsViewController * logisticsVC = [[OrderLogisticsViewController alloc]initOrderId:orderId withVCType:LogisticsVCType_idOrMatn];
    [self pushViewController:logisticsVC];
    
}

- (void)gotoAddSendInfo:(NSString *)orderId{
    
    AddSendInfoViewController * sendInfoVC = [[AddSendInfoViewController alloc]initWithOrderId:orderId withType:AddSendInfoVCType_idOrMatn];
    [self pushViewController:sendInfoVC];
}
- (void)gotoPaymentVC:(NSString *) orderId{
    
    
}

- (void)confirmGoods:(NSString *)orderId{
    
    [self showAlertView:@"确定已经收到货了吗?" withConfirmAction:^(UIAlertAction *action) {
        [self requestCancelOrder:@"40" withOrderId:orderId];
    } withCancelAction:^(UIAlertAction *action) {
        
    }];
}
//
- (void)requestCancelOrder:(NSString *)status withOrderId:(NSString *)orderId{
    NSDictionary * params = @{@"order_id":orderId,@"token":UserInfo.userToken,@"status":status};
    
    [[MainApi sharedInstance] postPath:@"/Otherorder/confirmOrCancel" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        
        NSLog(@"%@===",responseObject);
        if (!error) {
            [self requestOrderList];
        }
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
